# src/routers/objects_field.py
from fastapi import APIRouter, Depends, HTTPException, Query
from sqlalchemy.orm import Session
from typing import List, Optional

from src.core.ormdb import get_db
from src.core import dependencies
from src.schemas.login_manager import AuthManager
from src.schemas import request_model
from src.crud import sys_objects_field as crud
from src.schemas import sys_objects_field as schemas
# ====================== 追加：objects-header ======================
from src.crud import sys_objects as sysObjectsCrud
from src.schemas import sys_objects as sysObjectsSchemas
# =============== 追加：字段规则 CURD ===============
from src.crud import sys_objects_field_rule as rule_crud
from src.schemas import sys_objects_field_rule as rule_schemas

from src.services.manages import objects as serverObjects

router = APIRouter()


@router.post("/objects", summary="新增单条对象头", response_model=request_model.ResponseModel)
def create_object_header(
        item: sysObjectsSchemas.SysObjectsCreate,
        db: Session = Depends(get_db),
        auth: AuthManager = Depends(dependencies.auth_token)
):
    return sysObjectsCrud.create_object(db, auth, item)


@router.post("/objects/batch", summary="批量新增对象头", response_model=request_model.ResponseModel)
def create_object_headers(
        items: List[sysObjectsSchemas.SysObjectsCreate],
        db: Session = Depends(get_db),
        auth: AuthManager = Depends(dependencies.auth_token)
):
    return sysObjectsCrud.create_objects(db, auth, items)


@router.get("/objects", summary="分页查询对象头", response_model=request_model.ResponseModel)
def read_object_headers(
        page: int = 1,
        page_size: int = 100,
        db: Session = Depends(get_db),
        auth: AuthManager = Depends(dependencies.auth_token)
):
    return sysObjectsCrud.get_objects(db, auth, page, page_size)


@router.post("/objects/query", summary="条件查询对象头", response_model=request_model.ResponseModel)
def query_object_headers(
        query: request_model.QueryModel | None = None,
        db: Session = Depends(get_db),
        auth: AuthManager = Depends(dependencies.auth_token)
):
    return sysObjectsCrud.get_objects(db, auth, filter_model=query)


@router.put("/objects/{pk}", summary="更新单条对象头")
def update_object_header(
        pk: str,
        item: sysObjectsSchemas.SysObjects,
        db: Session = Depends(get_db),
        auth: AuthManager = Depends(dependencies.auth_token)
):
    return sysObjectsCrud.update_object(db, auth, pk, item)


@router.put("/objects/batch", summary="批量更新对象头", response_model=request_model.ResponseModel)
def update_object_headers(
        model: request_model.UpdateModel,
        db: Session = Depends(get_db),
        auth: AuthManager = Depends(dependencies.auth_token)
):
    return sysObjectsCrud.update_objects(db, auth, model)


@router.delete("/objects/batch", summary="删除对象头", response_model=request_model.ResponseModel)
def delete_object_headers(
        model: request_model.DeleteModel,
        db: Session = Depends(get_db),
        auth: AuthManager = Depends(dependencies.auth_token)
):
    return sysObjectsCrud.delete_objects(db, auth, model)


# ---------- 单条新增 ----------
@router.post("/object-fileds", summary="新增单条对象字段", response_model=request_model.ResponseModel)
def create_object_field(
        item: schemas.SysObjectsFieldCreate,
        db: Session = Depends(get_db),
        auth: AuthManager = Depends(dependencies.auth_token)
):
    return crud.create_object_field(db, auth, item)


# ---------- 批量新增 ----------
@router.post("/object-fileds/batch", summary="批量新增对象字段", response_model=request_model.ResponseModel)
def create_object_fields(
        items: List[schemas.SysObjectsFieldCreate],
        db: Session = Depends(get_db),
        auth: AuthManager = Depends(dependencies.auth_token)
):
    return crud.create_object_fields(db, auth, items)


# ---------- 分页查询 ----------
@router.get("/object-fileds", summary="分页查询对象字段", response_model=request_model.ResponseModel)
def read_object_fields(
        page: int = 1,
        page_size: int = 100,
        db: Session = Depends(get_db),
        auth: AuthManager = Depends(dependencies.auth_token)
):
    return crud.get_object_fields(db, auth, page, page_size)


# ---------- 条件查询 ----------
@router.post("/object-fileds/query", summary="条件查询对象字段", response_model=request_model.ResponseModel)
def query_object_fields(
        query: request_model.QueryModel | None = None,
        db: Session = Depends(get_db),
        auth: AuthManager = Depends(dependencies.auth_token)
):
    print(query)
    return crud.get_object_fields(db, auth, filter_model=query)


# ---------- 单条更新 ----------
@router.put("/object-fileds/{pk}", summary="更新单条对象字段")
def update_object_field(
        pk: str,
        item: schemas.SysObjectsField,
        db: Session = Depends(get_db),
        auth: AuthManager = Depends(dependencies.auth_token)
):
    return crud.update_object_field(db, auth, pk, item)


# ---------- 批量更新 ----------
@router.put("/object-fileds/batch", summary="批量更新对象字段", response_model=request_model.ResponseModel)
def update_object_fields(
        model: request_model.UpdateModel,
        db: Session = Depends(get_db),
        auth: AuthManager = Depends(dependencies.auth_token)
):
    return crud.update_object_fields(db, auth, model)


# ---------- 删除 ----------
@router.delete("/object-fileds/batch", summary="删除对象字段", response_model=request_model.ResponseModel)
def delete_object_fields(
        model: request_model.DeleteModel,
        db: Session = Depends(get_db),
        auth: AuthManager = Depends(dependencies.auth_token)
):
    return crud.delete_object_fields(db, auth, model)


# ---------- 单条新增 ----------
@router.post("/field-rule", summary="新增单条字段规则", response_model=request_model.ResponseModel)
def create_field_rule(
        item: rule_schemas.SysObjectsFieldRuleCreate,
        db: Session = Depends(get_db),
        auth: AuthManager = Depends(dependencies.auth_token)
):
    return rule_crud.create_field_rule(db, auth, item)


# ---------- 批量新增 ----------
@router.post("/field-rule/batch", summary="批量新增字段规则", response_model=request_model.ResponseModel)
def create_field_rules(
        items: List[rule_schemas.SysObjectsFieldRuleCreate],
        db: Session = Depends(get_db),
        auth: AuthManager = Depends(dependencies.auth_token)
):
    return rule_crud.create_field_rules(db, auth, items)


# ---------- 分页查询 ----------
@router.get("/field-rule", summary="分页查询字段规则", response_model=request_model.ResponseModel)
def read_field_rules(
        page: int = 1,
        page_size: int = 100,
        db: Session = Depends(get_db),
        auth: AuthManager = Depends(dependencies.auth_token)
):
    return rule_crud.get_field_rules(db, auth, page, page_size)


# ---------- 条件查询 ----------
@router.post("/field-rule/query", summary="条件查询字段规则", response_model=request_model.ResponseModel)
def query_field_rules(
        query: request_model.QueryModel | None = None,
        db: Session = Depends(get_db),
        auth: AuthManager = Depends(dependencies.auth_token)
):
    return rule_crud.get_field_rules(db, auth, filter_model=query)


# ---------- 单条更新 ----------
@router.put("/field-rule/{pk}", summary="更新单条字段规则")
def update_field_rule(
        pk: str,
        item: rule_schemas.SysObjectsFieldRule,
        db: Session = Depends(get_db),
        auth: AuthManager = Depends(dependencies.auth_token)
):
    return rule_crud.update_field_rule(db, auth, pk, item)


# ---------- 批量更新 ----------
@router.put("/field-rule/batch", summary="批量更新字段规则", response_model=request_model.ResponseModel)
def update_field_rules(
        model: request_model.UpdateModel,
        db: Session = Depends(get_db),
        auth: AuthManager = Depends(dependencies.auth_token)
):
    return rule_crud.update_field_rules(db, auth, model)


# ---------- 删除 ----------
@router.delete("/field-rule/batch", summary="删除字段规则", response_model=request_model.ResponseModel)
def delete_field_rules(
        model: request_model.DeleteModel,
        db: Session = Depends(get_db),
        auth: AuthManager = Depends(dependencies.auth_token)
):
    return rule_crud.delete_field_rules(db, auth, model)
